/*!
//      
//      \file	k2k_IO.h
//      
//      \author	Copyright 2010 Valentina Vuksic <vuksv1@bfh.ch>, Beat Liechti <liecb2@bfh.ch>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//
//		Abstract Data Type for IO with the carme / orchid board
//      
//      
//      \brief		CAS-EBX-03 Projektarbeit 
//      			Valentina Vuksic (VUKSV1), Beat Liechti (LIECB2)
//      			IO ADT for the k2k (Kaffee2000) application
//      \version	1.0
//      \date		2010-04-06
//      \remark		Last Modifications:
//      \remark  	v1.0, VUKSV1/LIECB2, 2010-04-10 Initial release
//
*/

#ifndef __k2k_IO__
#define __k2k_IO__

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <time.h>
#include <fcntl.h>

#include "k2k_GlobalDefines.h"
#include "k2k_GlobalTypes.h"
#include "k2k_StateMachineTypes.h"

#if defined(X86)
#include "k2k_X86IO.h"
#endif
#include "k2k_LED.h"

#define MEM_DEV			"/dev/mem"

#define k2k_IO_ERR_SUCCESS					0x00000000
#define k2k_IO_ERR_OPEN_DEV_DISPLAY_FAILED	0xFFFF0001
#define k2k_IO_ERR_OPEN_DEV_BUTTON_FAILED	0xFFFF0002
#define k2k_IO_ERR_CREATE_CARMEIO			0xFFFF0003
#define k2k_IO_ERR_CREATE_ORCHIDIO			0xFFFF0004
#define k2k_IO_ERR_CREATE_X86IO				0xFFFF0005

/*!
// k2k_IO is a ADT to simplify IO with the targets (carme, orchid, x86)
// by "delegating" IO functions to the respective board functions 
// \brief IO ADT
*/				
typedef struct k2k_IO
{
#if defined(CARME) || defined (ORCHID)	
	/*! file descriptor for the display/LED device */
	int fd_display;
	/*! file descriptor for the button/switch device */
	int fd_button;	
#elif defined(X86)
	/*! pointer to the X86 IO structure */
	k2k_X86IO *x86IO;
#endif	
	/*! error code to signal initialization errors */
	int err;
} k2k_IO;

/*!
//
// Creates IO ADT "object"
// 
// \return pointer to the IO ADT
//
*/
k2k_IO* k2k_IO_Create();

/*!
//
// Releases IO ADT "object"
// 
// \param IO is the pointer to the IO structure
// 
// \return noting
//
*/
void k2k_IO_Release(k2k_IO *IO);

/*!
//
// Write a pattern to the LEDs
//
// \param IO is the pointer to the IO structure
// \param pattern is the bit map of the pattern
// 
// \return nothing
//
*/
void k2k_IO_WriteLed(k2k_IO *IO, UINT8 pattern);

/*!
//
// Create event from the values of the switches
// 
// \param IO is the pointer to the IO structure
//
// \return k2k_Event where EVENT_MAX means that no event has happened
//
*/
k2k_Event k2k_IO_GetEvent(k2k_IO *IO);

/*!
//
// Set the LEDs according to the state
//
// \param IO is the pointer to the IO structure
// \param currentState is the current state
// 
// \return nothing
//
*/
void k2k_IO_SetLeds(k2k_IO *IO, k2k_State currentState);

/*!
//
// Do some light show with the LEDs
//
// \param IO is the pointer to the IO structure
// \param direction is 0 for r2l and >0 for l2r
// \param loops is how many loops
// \param speed is how fast to move the LEDs (higher values ==> slower)
// 
// \return nothing
//
*/
void k2k_IO_LightShow(k2k_IO *IO, UINT8 direction, int loops, int speed);

/*!
//
// Do some random light show with the LEDs
//
// \param IO is the pointer to the IO structure
// \param loops is how many loops
// \param speed is how fast to move the LEDs (higher values ==> slower)
// 
// \return nothing
//
*/
void k2k_IO_RandomLightShow(k2k_IO *IO);


#endif
